package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.InuranceMessageBean;
import com.oscar.ehome.bean.pagebean.WorkMessageBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.view.InsuranceView;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/1
 * 保险档案详情
 */

public class InsuranceMessageActivity extends BasesActivity<InsuranceView, PagePresenter> implements InsuranceView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 主题色背景
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * 添加
     */
    @BindView(R.id.home_tv_num)
    TextView home_tv_num;
    /**
     * 添加
     */
    @BindView(R.id.title_right_layput)
    RelativeLayout rightLayout;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 公司
     */
    @BindView(R.id.campanymessage_text)
    TextView campanymessageText;
    /**
     * 保单号
     */
    @BindView(R.id.baodanmessage_text)
    TextView baodanmessageText;
    /**
     * 保单类型
     */
    @BindView(R.id.baodantypemessage_text)
    TextView baodantypemessageText;
    /**
     * 投保人
     */
    @BindView(R.id.toubaopoplemessage_text)
    TextView toubaopoplemessageText;
    /**
     * 投保人电话
     */
    @BindView(R.id.toubaopople_phonemessage_text)
    TextView toubaopoplePhonemessageText;
    /**
     * 被保险人
     */
    @BindView(R.id.bei_insuar_poplemessage_text)
    TextView beiInsuarPoplemessageText;
    /**
     * 受益人
     */
    @BindView(R.id.shouyi_peoplemessage_text)
    TextView shouyiPeoplemessageText;
    /**
     * 银行
     */
    @BindView(R.id.blankmessage_text)
    TextView blankmessageText;
    /**
     * 银行账号
     */
    @BindView(R.id.blank_numbermessage_text)
    TextView blankNumbermessageText;
    /**
     * 产品名字
     */
    @BindView(R.id.chanpin_namemessage_text)
    TextView chanpinNamemessageText;
    /**
     * 承保时间
     */
    @BindView(R.id.chengb_timemessage_text)
    TextView chengbTimemessageText;
    /**
     * 年交保费
     */
    @BindView(R.id.year_costmessage_text)
    TextView yearCostmessageText;
    /**
     * 缴费期间
     */
    @BindView(R.id.cost_timemessage_text)
    TextView costTimemessageText;
    /**
     * 保障期间
     */
    @BindView(R.id.baozhang_timemessage_text)
    TextView baozhangTimemessageText;
    /**
     * 保额
     */
    @BindView(R.id.baoemessage_text)
    TextView baoemessageText;
    /**
     * 保险责任
     */
    @BindView(R.id.insura_dutymessage_text)
    TextView insuraDutymessageText;

    PagePresenter presenter;
    String token = "";
    String id = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurancemessage);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(InsuranceMessageActivity.this).getSystemConfig().getColor()));
        initView();
        initData();
    }

    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    /**
     *
     */
    public void initView() {

        id = getIntent().getStringExtra("id");

        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("查看档案");
        home_tv_num.setVisibility(View.VISIBLE);
        rightLayout.setVisibility(View.VISIBLE);
        home_tv_num.setText("编辑");

        token = SystemConfigFactory.getInstance(InsuranceMessageActivity.this).getSystemConfig().getToken();

    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            if (id != null) {
                presenter.getInsuranceDetial(token, id);
                showLoading();
            } else {
                initMesage();
            }
        } else {
            initMesage();
        }
    }


    @Override
    public void upload_mesage(String result) {

    }

    @Override
    public void upload_worklog(String result) {

    }

    /**
     * 保险详情
     *
     * @param result
     */
    @Override
    public void getInsuranceMessage(InuranceMessageBean result) {
        dismissLoading();
        if (result != null) {
            Gson gson = new Gson();
            String jsonStr = gson.toJson(result);
            SystemConfigFactory.getInstance(InsuranceMessageActivity.this).getSystemConfig().setInsurance(jsonStr);

            id = result.getId();
            campanymessageText.setText(result.getCompanyName());
            baodanmessageText.setText(result.getPolicyNumber());
            baodantypemessageText.setText(result.getPolicyStatus());
            toubaopoplemessageText.setText(result.getInsuredPerson());
            toubaopoplePhonemessageText.setText(result.getInsuredPersonMobile());
            beiInsuarPoplemessageText.setText(result.getInsured());
            shouyiPeoplemessageText.setText(result.getBeneficiaries());
            blankmessageText.setText(result.getTransferBank());
            blankNumbermessageText.setText(result.getTransferAccountNumber());
            chanpinNamemessageText.setText(result.getProductName());
            chengbTimemessageText.setText(result.getUnderwritingTimeStr());
            yearCostmessageText.setText(result.getAnnualPremium());
            costTimemessageText.setText(result.getPaymentPeriod());
            baozhangTimemessageText.setText(result.getGuaranteePeriod());
            baoemessageText.setText(result.getInsuredAmount());
            insuraDutymessageText.setText(result.getInsuranceDuty());
        }
    }

    @Override
    public void getWorkMessage(WorkMessageBean result) {

    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    /**
     * 保险信息
     */
    public void initMesage() {
        campanymessageText.setText(getString(R.string.nodata));
        baodanmessageText.setText(getString(R.string.nodata));
        baodantypemessageText.setText(getString(R.string.nodata));
        toubaopoplemessageText.setText(getString(R.string.nodata));
        toubaopoplePhonemessageText.setText(getString(R.string.nodata));
        beiInsuarPoplemessageText.setText(getString(R.string.nodata));
        shouyiPeoplemessageText.setText(getString(R.string.nodata));
        blankmessageText.setText(getString(R.string.nodata));
        blankNumbermessageText.setText(getString(R.string.nodata));
        chanpinNamemessageText.setText(getString(R.string.nodata));
        chengbTimemessageText.setText(getString(R.string.nodata));
        yearCostmessageText.setText(getString(R.string.nodata));
        costTimemessageText.setText(getString(R.string.nodata));
        baozhangTimemessageText.setText(getString(R.string.nodata));
        baoemessageText.setText(getString(R.string.nodata));
        insuraDutymessageText.setText(getString(R.string.nodata));
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @OnClick({R.id.title_layout_left, R.id.title_right_layput})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left:
                finish();
                break;
            case R.id.title_right_layput:
                Intent intent = new Intent(this, InsurawriteActivity.class);
                intent.putExtra("add", "2");
                intent.putExtra("id", id);
                startActivity(intent);
                break;
        }
    }
}
